// camera.js
const sketch = require('../../utils/sketch.js')
Page({
  data: {
    cameraContext: null,
    devicePosition: 'back',
    sketchImagePath: ''
  },
  onLoad: function () {
    this.setData({
      cameraContext: wx.createCameraContext()
    })
  },
  takePhoto: function () {
    const { cameraContext } = this.data
    cameraContext.takePhoto({
      quality: 'high',
      success: (res) => {
        const tempImagePath = res.tempImagePath
        this.processImage(tempImagePath)
      }
    })
  },
  processImage: function (imagePath) {
    sketch.convertToSketch(imagePath).then(sketchPath => {
      this.setData({
        sketchImagePath: sketchPath
      })
    })
  },
  switchCamera: function () {
    this.setData({
      devicePosition: this.data.devicePosition === 'back' ? 'front' : 'back'
    })
  },
  saveImage: function () {
    wx.saveImageToPhotosAlbum({
      filePath: this.data.sketchImagePath,
      success: (res) => {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
      }
    })
  },
  backToHome: function () {
    wx.navigateBack()
  }
})
